package com.hgy.sparsearray;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class SparseArray {

    public static void main(String[] args){
        //创建一个原始的二维数组11*11
        //0:表示没有棋子，1:表示黑子，2:表示蓝子
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 2;
        //输出原始的二维数组
        System.out.println("原始的二维数组~~");
        for (int[] row:chessArr1) {
            for (int data:row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }

        //将二维数组 转 稀疏数组
        //1.先遍历二维数组 得到非0数据的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if(chessArr1[i][j]!=0){
                    sum++;
                }
            }
        }

        //2.创建对应的稀疏数组
        int sparseArr[][] = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

        //遍历二维数组，将非0的值存放到sparseArr中
        int count = 0;//表示二维数组非0值个数
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j]!=0 && count<sum){
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }

        //输出稀疏数组的形式
        System.out.println();
        System.out.println("得到稀疏数组为~~~~");
        for (int i = 0; i < sum+1; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.printf("%d\t", sparseArr[i][j]);
            }
            System.out.println();
        }

        //将稀疏数组还原为原始的二维数组

        //1.先读取稀疏数组的第一行，根据第一行的数据，创建原始的二维数组

        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

        //2.在读取稀疏数组后几行的数据，并赋给原始的二维数组

        for (int i = 1; i < sparseArr.length; i++) {
            chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
        }

        System.out.println("恢复后的二维数组~~~");
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                System.out.printf("%d\t", chessArr2[i][j]);
            }
            System.out.println();
        }

        //将稀疏数组存入文件map.data中
        try{
            FileOutputStream fos = new FileOutputStream("map.data");
            for(int i = 0; i < sparseArr.length; i++){
                fos.write(sparseArr[i][0]);
                fos.write(sparseArr[i][1]);
                fos.write(sparseArr[i][2]);
                System.out.println(sparseArr[i][2]);
            }
        }catch (IOException e){
            e.printStackTrace();
        }

        //从文件map.data中读取稀疏数组
        int[][] sparseArr1;
        try {
            FileInputStream fis = new FileInputStream("map.data");
            int row = fis.read();
            int col = fis.read();
            int c = fis.read();
            sparseArr1 = new int[c+1][3];
            sparseArr1[0][0] = row;
            sparseArr1[0][1] = col;
            sparseArr1[0][2] = c;
            for (int i = 1; i < sparseArr1.length; i++) {
                for (int j = 0; j < 3; j++) {
                    sparseArr1[i][j] = fis.read();
                }
            }
            //输出从文件读取的稀疏数组
            System.out.println();
            System.out.println("读取文件得到稀疏数组为~~~~");
            for (int i = 0; i < sparseArr1.length; i++) {
                for (int j = 0; j < 3; j++) {
                    System.out.printf("%d\t", sparseArr1[i][j]);
                }
                System.out.println();
            }
        }catch (IOException e){
            e.printStackTrace();
        }



    }
}
